<?php
function getUsers($dbname){
	$html=null;	
	$cambio_db_exitoso=true;
	//$sybmas_connection=start_sybmas_connection();
	$sybmas_connection=start_default_sybuser_connection();
	if(!empty($dbname)){
		$cambio_bd_exitoso=@sybase_select_db($dbname,$sybmas_connection);
	}
	if($cambio_db_exitoso){
		$query="SELECT name FROM ".$dbname."..sysusers ".
			"WHERE uid BETWEEN 1 AND 16383 ".
			"AND name <> '".obtener_usuario_db()."' ".
			"ORDER BY name";
		//echo $query;
		$result=@sybase_query($query,$sybmas_connection);
		while($data=sybase_fetch_array($result)){
			$name=$data['name'];	
			if($name==obtener_nombre_usuario()){
				continue;
			}				
			$html.="<option value='".$name."'>$name</option>\n";
		}	
	}	
	sybase_close($sybmas_connection);		
	return $html;
}

function getGroups($dbname){
	$html=null;
	$cambio_db_exitoso=true;
	//$sybmas_connection=start_sybmas_connection();
	$sybmas_connection=start_default_sybuser_connection();	
	if(!empty($dbname)){
		$cambio_bd_exitoso=@sybase_select_db($dbname,$sybmas_connection);
	}
	if($cambio_db_exitoso){
		$query="SELECT name FROM ".$dbname."..sysusers ".
			"WHERE (uid > 16383 OR uid=0) ".
			"AND name NOT IN ('sa_role','sso_role','oper_role','sybase_ts_role','navigator_role','replication_role') ".
			"ORDER BY name";
		//echo $query;
		$result=@sybase_query($query,$sybmas_connection);
		while($data=sybase_fetch_array($result)){
			$name=$data['name'];
			$html.="<option value='".$name."'>$name</option>\n";
		}		
	}
	sybase_close($sybmas_connection);		
	return $html;
}

function getPrivileges($dbname,$tableowner,$tablename,$user){	
	$html=null;
	$cambio_db_exitoso=true;
	//$sybmas_connection=start_sybmas_connection();
	$sybmas_connection=start_default_sybuser_connection();	
	if(!empty($dbname)){
		sybase_set_message_handler("void_msg_handler",$sybmas_connection);
		$cambio_bd_exitoso=@sybase_select_db($dbname,$sybmas_connection);
	}
	if($cambio_db_exitoso){
		$query="sp_helprotect \"$tableowner.$tablename\",\"$user\"";
		sybase_set_message_handler("void_msg_handler",$sybmas_connection);
		$result=@sybase_query($query,$sybmas_connection);
		sybase_set_message_handler("msg_handler",$sybmas_connection);
		$firstLoop=true;
		while($data=sybase_fetch_array($result)){
			if(!array_key_exists('action',$data)){
				continue;
			}
			$type=strtoupper($data['type']);
			$column=strtolower($data['column']);
			$privilege=strtoupper($data['action']);
			if($type=='GRANT'&&$column=='all'){							
				if(!$firstLoop){
					$html.=",";
				}
				$html.=$privilege;
				$firstLoop=false;
			}
		}		
	}
	sybase_close($sybmas_connection);		
	return trim($html);
}

function getPrivilegesWithGrantOption($dbname,$tableowner,$tablename,$user){
	$html=null;
	$cambio_db_exitoso=true;
	//$sybmas_connection=start_sybmas_connection();
	$sybmas_connection=start_default_sybuser_connection();	
	if(!empty($dbname)){
		sybase_set_message_handler("void_msg_handler",$sybmas_connection);
		$cambio_bd_exitoso=@sybase_select_db($dbname,$sybmas_connection);
	}
	if($cambio_db_exitoso){
		$query="sp_helprotect \"$tableowner.$tablename\",\"$user\"";
		sybase_set_message_handler("void_msg_handler",$sybmas_connection);
		$result=@sybase_query($query,$sybmas_connection);
		sybase_set_message_handler("msg_handler",$sybmas_connection);
		$firstLoop=true;
		//echo $query;
		while($data=sybase_fetch_array($result)){
			if(!array_key_exists('action',$data)){
				continue;
			}
			$type=strtoupper($data['type']);
			$column=strtolower($data['column']);
			$privilege=strtoupper($data['action']);
			$grantable=$data['grantable'];			
			if($type=='GRANT' && $column=='all' && $grantable=='TRUE'){							
				if(!$firstLoop){
					$html.=",";
				}
				$html.=$privilege;
				$firstLoop=false;
			}
		}		
	}
	//sybase_close($sybmas_connection);		
	return $html;
}



function insertIntoLog($grantee,$type,$action,$dbname,$tableid){
	
	if(!$dbname){
		$dbname=$_SESSION['sybmas.user.dbname'];
	}
	
	$suid=$_SESSION['sybmas.user.suid'];
	
	$sybmas_user_connection=start_default_sybuser_connection();
	
	//$query="SELECT uid FROM $dbname..sysusers WHERE suid=$suid";
	//$result=sybase_query($query,$sybmas_user_connection);
	//$data=sybase_fetch_row($result);
	$id_grantor=$suid;
	
	
	$query="SELECT suid FROM $dbname..sysusers WHERE name='$grantee'";
	$result=sybase_query($query,$sybmas_user_connection);
	$data=sybase_fetch_row($result);
	$id_grantee=$data[0];
	//echo "<h1>$id_grantee</h1>";
						
	switch ($type){
		case 'GRANT':
			$id_type=1;
			break;
		case 'REVOKE':
			$id_type=2;
			break;			
		default:
			die("El tipo $type no es reconocido");
			break;
	}			
				
	switch($action){
		case 'ALL':
			$id_action=0;
			break;
		case 'ALTER':
			$id_action=1;
			break;
		case 'DELETE':
			$id_action=2;
			break;
		case 'INSERT':
			$id_action=3;
			break;
		case 'REFERENCES':
			$id_action=4;
			break;
		case 'SELECT':
			$id_action=5;
			break;
		case 'UPDATE':
			$id_action=6;
			break;
		default;
			die("La accion $action no esta reconocida");
			break;
	}
	
	sybase_min_server_severity(11);
	$sybmas_connection=start_sybmas_connection();
	
	$query="SELECT dbid FROM master..sysdatabases WHERE name='$dbname'";
	$result=sybase_query($query,$sybmas_connection);
	$data=sybase_fetch_row($result);
	$dbid=$data[0];
	
	$query="SELECT ISNULL(MAX(id_permiso),0)+1 FROM ".SYBMAS_DATABASE_NAME."..sybmas_permisoObjeto";
	$result=sybase_query($query,$sybmas_connection);
	$data=sybase_fetch_row($result);
	$id_permiso=$data[0];
	
	$query="INSERT INTO ".SYBMAS_DATABASE_NAME."..sybmas_permisoObjeto(id_permiso,suid_otorga,suid_recibe,dbid,tableid,fecha,id_tipoPermiso,id_accion) ".
	"VALUES( $id_permiso,$id_grantor,$id_grantee,$dbid,$tableid,getDate(),$id_type,$id_action)";
	$result=sybase_query($query,$sybmas_connection);
	
	//echo $query."<br>";
	
	//sybase_close($sybmas_connection);
}
?>